package org.putrpctorest.expression;

import java.util.HashMap;
import java.util.Map;

import org.putrpctorest.PutRpcToRestError;
import org.w3c.dom.Element;

public class ExpressionFactory {
	
	static {

		ExpressionFactory.getInstance().registerParser(
				new ParameterGroupParser());
		
		ExpressionFactory.getInstance().registerParser(
				new MethodCallParser());
	}

	
	private static ExpressionFactory instance;
	
	public static ExpressionFactory getInstance() {
		if (instance == null) {
			instance = new ExpressionFactory();
			
		}
		return instance;
	}
	
	private Map<String,ExpressionParser> serviceRouterParserRegister = new HashMap<String, ExpressionParser>();
	
	public AbstractExpression createServiceRouter(String type, String id, Element config) {
		ExpressionParser parser = serviceRouterParserRegister.get(type);
		if (parser == null) {
			throw new PutRpcToRestError("Could not find router parser for type:" + type);
		}
		return parser.parseRouter(id,config);
	}
	
	public void registerParser(ExpressionParser parser) {
		serviceRouterParserRegister.put(parser.getType(), parser);
	}
	
}
